package com.brillilab.common.utils;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.common.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisLockUtils {

    private static RedisCache redisCache;

    public static void unlock(String lockKey) {
        if(redisCache.getObj(lockKey) == null){
            throw new BrillilabException(ResultEnum.LOCK_NOT_RELEASE);
        }
        redisCache.delete(lockKey);
    }

    public static boolean lock(String lockKey) {
        if(redisCache.getObj(lockKey) != null){
            return false;
        }
        redisCache.set(lockKey,true);
        return true;
    }

    @Autowired
    public void setRedisCache(RedisCache redisCache) {
        RedisLockUtils.redisCache=redisCache;
    }
}
